package com.itlong.cloud.tapd.util;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <des>
 *统计月度单数 类型4
 * <des>
 *
 * @create Date 2021-09-09
 **/
public class GetMonthlyTaskCount {

    public static HashMap<String, Integer> getCount(String user,List<HashMap<String, Object>> taskList, List<HashMap<String, Object>> storyList, List<HashMap<String, Object>> bugList) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date firstDayofLastMonth = format.parse(GetTime.getFirstDayOfLastMonth());
        HashMap<String, Integer> map = new HashMap<>();
        int count = 0;
        int count1 = 0;
        if(taskList.size()>0) {
            for (HashMap<String, Object> taskmap : taskList) {
                if (StringUtils.isBlank((String) taskmap.get("due")) ||StringUtils.isBlank((String) taskmap.get("owner"))) {
                    continue;
                }
                String owner = (String) taskmap.get("owner");
                Date due = format.parse((String) taskmap.get("due"));
                Date completed = format.parse((String) taskmap.get("completed"));
                if (owner.contains(user) && due.compareTo(completed) >= 0) {
                    count++;
                }
                if (owner.contains(user) && due.compareTo(completed) < 0) {
                    count1++;
                }
            }
        }
        if(storyList.size()>0) {
            for (HashMap<String, Object> storymap : storyList) {
                if (StringUtils.isBlank((String) storymap.get("due"))||StringUtils.isBlank((String) storymap.get("owner"))) {
                    continue;
                }
                String owner = (String) storymap.get("owner");
                Date due = format.parse((String) storymap.get("due"));
                Date completed = format.parse((String) storymap.get("completed"));
                if (owner.contains(user) && due.compareTo(completed) >= 0) {
                    count++;
                }
                if (owner.contains(user) && due.compareTo(completed) < 0) {
                    count1++;
                }
            }
        }
        if(bugList.size()>0) {
            for (HashMap<String, Object> bugmap : bugList) {
                if (StringUtils.isBlank((String) bugmap.get("due"))||StringUtils.isBlank((String) bugmap.get("owner"))) {
                    continue;
                }
                String owner = (String) bugmap.get("owner");
                Date due = format.parse((String) bugmap.get("due"));
                Date resolved = format.parse((String) bugmap.get("resolved"));
                if (owner.contains(user) && due.compareTo(resolved) >= 0) {
                    count++;
                }
                if (owner.contains(user) && due.compareTo(resolved) < 0) {
                    count1++;
                }
            }
        }
        map.put("count",count);
        map.put("count1",count1);
        return map;
    }
}